<template>
	<view class="container">
		<view class="form">
			<uni-forms ref="formRef" :model="form" :rules="rules" label-position="top">
				<uni-forms-item label="参数名称" name="configName" required>
					<uni-easyinput v-model="form.configName" placeholder="请输入参数名称" />
				</uni-forms-item>
				<uni-forms-item label="参数键名" name="configKey" required>
					<uni-easyinput v-model="form.configKey" placeholder="请输入参数键名" />
				</uni-forms-item>
				<uni-forms-item label="参数键值" name="configValue" required>
					<uni-easyinput v-model="form.configValue" placeholder="请输入参数键值" />
				</uni-forms-item>
				<uni-forms-item label="系统内置" name="configType" required>
					<xy-checkbox v-model="form.configType" :data="sys_yes_no" placeholder="系统内置" clearable />
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="form.remark" placeholder="请输入备注" />
				</uni-forms-item>
				<uni-forms-item>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="submitForm()">确认</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="cancel()">取消</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app'
	import { listConfig, getConfig, delConfig, addConfig, updateConfig, refreshCache } from "@/pages_work/api/system/config";
	const {
		proxy
	} = getCurrentInstance();
	const { sys_yes_no } = proxy.useDict("sys_yes_no");

	/** 表单数据 */
	const form = ref({});

	/** 表单检验规则 */
	const rules = ref(proxy.uniappRules({
		configName: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
		configKey: [{ required: true, message: "参数键名不能为空", trigger: "blur" }],
		configValue: [{ required: true, message: "参数键值不能为空", trigger: "blur" }]
	}))

	onLoad((params) => {
		uni.setNavigationBarTitle({
			title: params.title
		});
		if (params.configId == undefined || params.configId == null || params.configId == '') {
		} else {
			handleUpdate(params);
		}
	})

	/** 取消按钮 */
	const cancel = () => {
		uni.navigateBack()
	}

	/** 提交按钮 */
	const submitForm = () => {
		proxy.$refs["formRef"].validate().then(valid => {
			if (valid) {
				if (form.value.configId != undefined) {
					updateConfig(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						setTimeout(uni.navigateBack, 1000);
					});
				} else {
					addConfig(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						setTimeout(uni.navigateBack, 1000);
					});
				}
			}
		});
	};
	
	/** 修改角色 */
	const handleUpdate = (row) => {
		const id = row.configId;
		getConfig(id).then(response => {
			form.value = response.data;
		});
	}
	
	onUnload(() => {
		uni.$emit('update')
	})
	
	
</script>

<style lang="scss">
	@import "@/static/scss/work/form.scss";
</style>
